﻿using System.Collections.Generic;
using UnityEngine;
using System;

namespace HBFramework
{
    /// <summary>
    /// 消息管理
    /// </summary>
    public partial class MessageManager : Singleton<MessageManager>, ISingleton
    {
        private Dictionary<int, Action> messageDict;
        private Dictionary<int, Action<object[]>> messageDict_objs;
        private Dictionary<int, Action<int>> messageDict_int;
        private Dictionary<int, Action<int[]>> messageDict_ints;
        private Dictionary<int, Action<float>> messageDict_float;
        private Dictionary<int, Action<float[]>> messageDict_floats;
        private Dictionary<int, Action<string>> messageDict_string;
        private Dictionary<int, Action<string[]>> messageDict_strings;
        private Dictionary<int, Action<Message>> messageDict_message;

        void ISingleton.OnInit()
        {
            messageDict = new Dictionary<int, Action>();
            messageDict_objs = new Dictionary<int, Action<object[]>>();
            messageDict_int = new Dictionary<int, Action<int>>();
            messageDict_ints = new Dictionary<int, Action<int[]>>();
            messageDict_float = new Dictionary<int, Action<float>>();
            messageDict_floats = new Dictionary<int, Action<float[]>>();
            messageDict_string = new Dictionary<int, Action<string>>();
            messageDict_strings = new Dictionary<int, Action<string[]>>();
            messageDict_message = new Dictionary<int, Action<Message>>();
        }

        void ISingleton.OnDestroy()
        {
            messageDict.Clear();
            messageDict_objs.Clear();
            messageDict_int.Clear();
            messageDict_ints.Clear();
            messageDict_float.Clear();
            messageDict_floats.Clear();
            messageDict_string.Clear();
            messageDict_strings.Clear();
            messageDict_message.Clear();
        }

        /// <summary>
        /// 发消息
        /// </summary>
        /// <param name="messageId"></param>
        /// <param name="args"></param>
        public void SendMessage(int messageId)
        {
            if (messageDict == null
                || !messageDict.ContainsKey(messageId))
            {
                DebugUtility.Log("MessageManager", $"messageDict中不包含{messageId}消息");
                return;
            }

            messageDict[messageId]?.Invoke();
        }

        /// <summary>
        /// 注册消息
        /// </summary>
        /// <param name="messageId"></param>
        /// <param name="act"></param>
        public void RegisterMessage(int messageId, Action act)
        {
            if (messageDict == null)
            {
                messageDict = new Dictionary<int, Action>();
            }

            if (!messageDict.ContainsKey(messageId))
            {
                messageDict.Add(messageId, null);
            }

            messageDict[messageId] += act;
        }

        /// <summary>
        /// 取消注册消息
        /// </summary>
        /// <param name="messageId"></param>
        /// <param name="act"></param>
        public void UnregisterMessage(int messageId, Action act)
        {
            if (messageDict == null
                || !messageDict.ContainsKey(messageId))
            {
                DebugUtility.LogWarning("MessageManager", $"messageDict中不包含{messageId}消息");
                return;
            }

            messageDict[messageId] -= act;
        }
    }
}